package com.jkgl.function;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jkgl.context.RedisStreamContext;
import com.jkgl.entity.MesPersistence;
import com.jkgl.enums.ReportEnum;
import com.jkgl.entity.SysUserEntity;
import com.jkgl.entity.UserInfoDayEntity;
import com.jkgl.entity.UserInfoSelfEntity;
import com.jkgl.mq.MessageProducer;
import com.jkgl.result.Report;
import com.jkgl.service.MesPersistenceService;
import com.jkgl.service.SysUserService;
import com.jkgl.service.UserInfoDayService;
import com.jkgl.service.UserInfoSelfService;
import com.jkgl.util.SendStreamUtil;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Component
@Slf4j
@RequiredArgsConstructor
public class DataFormatService {
    private final UserInfoSelfService userInfoSelf;
    private final UserInfoDayService userInfoDay;
    private final SysUserService userService;
    private final SendStreamUtil sendUtil;


    @XxlJob("WeekReportHandler")
    public void weekReport() {
        log.info("定时任务启动 开启周报数据收集");
//        正常用户
        QueryWrapper<SysUserEntity> userQuery = new QueryWrapper<>();
        userQuery.lambda().eq(SysUserEntity::getStatus, 0);
        List<SysUserEntity> userList = userService.list(userQuery);
//        遍历id --逐个处理 三个数组不好对应
        for (SysUserEntity user : userList) {
            //        转为Map
            Report report = new Report();
            report.setReportEnum(ReportEnum.WEEK);
            QueryWrapper<UserInfoSelfEntity> selfQuery = new QueryWrapper<>();
            selfQuery.lambda().eq(UserInfoSelfEntity::getUserId, user.getUserId());
            UserInfoSelfEntity self = userInfoSelf.getOne(selfQuery);
            if (self == null) {
//              TODO  发送邮件通知异常
                sendUtil.sendError(user,"用户周报异常  self 为空");
                continue;
            }
            report.setEmail(user.getEmail());
            report.setNickName(user.getNickName());
            report.setUserId(user.getUserId());
            report.setUserName(user.getUserName());
            log.info("转入mail模块==>>>>{}",user.getUserId());
//            TODO 发送到邮件模块
            sendUtil.transferMail(report);
        }
    }


    @XxlJob("DayReportHandler")
    public void dayReport(){
        log.info("日报开始生成");
//        正常用户
        QueryWrapper<SysUserEntity> userQuery = new QueryWrapper<>();
        userQuery.lambda().eq(SysUserEntity::getStatus,0);
        List<SysUserEntity> userList = userService.list(userQuery);
//        遍历id --逐个处理
        for(SysUserEntity user : userList){
            Report report = new Report();
            report.setReportEnum(ReportEnum.DAY);
            QueryWrapper<UserInfoSelfEntity> selfQuery = new QueryWrapper<>();
            selfQuery.lambda().eq(UserInfoSelfEntity::getUserId,user.getUserId());
            UserInfoSelfEntity self = userInfoSelf.getOne(selfQuery);
            if(self == null){
                sendUtil.sendError(user,"用户日报异常  self 为空");
//              TODO  发送邮件通知异常
                System.out.println(user+" 错误");
                continue;
            }
            QueryWrapper<UserInfoDayEntity> dayQuery = new QueryWrapper<>();
            dayQuery.lambda().eq(UserInfoDayEntity::getUserId,user.getUserId())
//                    定时任务十二点启动  日期往前推一天
                    .eq(UserInfoDayEntity::getCreateTime, LocalDate.now().minusDays(1));
            UserInfoDayEntity day = userInfoDay.getOne(dayQuery);
            if(day != null) {
                log.info("转入mail模块==>>>>{}", user.getUserId());
                report.setEmail(user.getEmail());
                report.setNickName(user.getNickName());
                report.setUserId(user.getUserId());
                report.setUserName(user.getUserName());
                sendUtil.transferMail(report);
//            TODO 发送report 到mail中处理
            }else{
                report.setEmail(user.getEmail());
                report.setNickName(user.getNickName());
                report.setUserId(user.getUserId());
                report.setUserName(user.getUserName());
                report.setDataSign(1);
                sendUtil.transferMail(report);
            }
        }
    }



    @XxlJob("SportNoticeBegin")
    public void noticeBegin(){
//        这里是执行类型 1早/2晚
        try {

            String jobParam = XxlJobHelper.getJobParam();
            if("null".equals(String.valueOf(jobParam))){
                sendUtil.sendError(jobParam,"运动提醒参数错误");
                return;
            }
            log.info("运动提醒---");
            QueryWrapper<SysUserEntity> userQuery = new QueryWrapper<>();
            userQuery.lambda().eq(SysUserEntity::getStatus,0);
            List<SysUserEntity> userList = userService.list(userQuery);
//        遍历id --逐个处理 三个数组不好对应
            for(SysUserEntity user : userList){
                QueryWrapper<UserInfoSelfEntity> selfQuery = new QueryWrapper<>();
                selfQuery.lambda().eq(UserInfoSelfEntity::getUserId,user.getUserId());
                UserInfoSelfEntity self = userInfoSelf.getOne(selfQuery);
                if(self == null){
                    sendUtil.sendError(user,"运动提醒  self 为空");
//              TODO  发送邮件通知异常
                    System.out.println(user+" 错误");
                    continue;
                }
                QueryWrapper<UserInfoDayEntity> dayQuery = new QueryWrapper<>();
                dayQuery.lambda().eq(UserInfoDayEntity::getUserId,user.getUserId())
                        .eq(UserInfoDayEntity::getCreateTime, LocalDate.now());
                UserInfoDayEntity day = userInfoDay.getOne(dayQuery);
//                没有过滤当日没有统计的 --再实现类中发送提醒
                sendUtil.sendSportNotice(Integer.valueOf(jobParam),day,self,user.getEmail());
            }
        }catch (Exception e){
            e.printStackTrace();
            sendUtil.sendError(e,"运动提醒 定时任务部分异常 ");
        }
    }
    static int i =0;
    @XxlJob("test1")
    public void test1(){
        System.out.println("执行了第"+ i++ +"次");
    }

    public static void main(String[] args) {
        System.out.println(LocalDate.now().minusDays(7));
        System.out.println(LocalDateTime.now());
    }
}
